home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0142_JPEG-JFIF Height & Width.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  2KB  |  72 lines

  1. {
  2. > I've asked this question before, in several message areas, but
  3. > have still to get an answer.. I need to be able to get the size
  4. > and colors from a JPEG/JFIF image file.. Nothing more, nothing
  5. > less... Structures would do, regardless of language (C, Asm,
  6. > Pas, Basic). Anyone?
  7.  
  8. Here it is (not fully tested, only extracts height and width of the picture!)
  9. }
  10.  
  11. Procedure GetJpegInfo(FName : String; VAR IsJpeg: Boolean; VAR Height,
  12.                             Width : Word);
  13.  
  14. {Checks if file FName is a (true) JPEG/JFIF file and extracts
  15.  height and width (in pixels) of the picture}
  16. Const
  17.   JFIFS : String[4] = #$FF + #$D8 + #$FF + #$E0;
  18.           {JFIF marker: $FF SOI $FF App0}
  19.  
  20. Var F : File;
  21.     ReadS : String;
  22.     ARead : Word;
  23.     Count : Integer;
  24.  
  25. begin
  26.    Assign(F,FName);
  27.    Reset(F,1);
  28.    Blockread(F, ReadS[1], 255, Aread);
  29.    ReadS[0] := Chr(Aread);
  30.    Close(F);
  31.  
  32.    IsJpeg := FALSE;
  33.  
  34.    {Search for JFIF marker in first 255 bytes of the file.
  35.     If NOT found, then you can safely assume the file isn't
  36.     a (real) JPEG/JFIF file}
  37.  
  38.    if Pos(JFIFS, ReadS) > 0 then
  39.       begin
  40.       If (Copy(ReadS, Pos(JFIFS,ReadS)+Length(JFIFS)+2,5) = 'JFIF'+#0) then
  41.          begin
  42.  
  43.          {We have a JPEG/JFIF File!}
  44.  
  45.          IsJpeg := TRUE;
  46.  
  47.          {Search for SOF marker}
  48.  
  49.          Count := 0;
  50.          Repeat
  51.           inc(Count);
  52.          Until (Count > length(ReadS)) OR
  53.                (ReadS[Count] in [#192..#207]);
  54.          if Count <= Length(ReadS) then
  55.             begin
  56.            { ReadS[Count] = first SOF marker
  57.              Count + 1 = length high byte  \ length of APP0 data!
  58.              Count + 2 = length low byte   /
  59.              Count + 3 = data precision    - colors (?)
  60.              Count + 4 = height high byte  \ heigth of picture
  61.              Count + 5 = height low byte   /
  62.              Count + 6 = width high byte   \ width of picture
  63.              Count + 7 = width low byte    /
  64.            }
  65.             Height := Word(Ord(ReadS[Count+4])*256) + Ord(ReadS[Count+5]);
  66.             Width  := Word(Ord(ReadS[Count+6])*256) + ord(ReadS[Count+7]);
  67.             end;
  68.          end;
  69.       end;
  70. end;
  71.  
  72.